package com.itstudy.elasticsearch.controller;

import com.itstudy.elasticsearch.service.ContentService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @ClassNameContentController
 * @Description TODO
 * @Author miaohuaixiang
 * @Date 2020/6/28 20:20
 * @Version 1.0
 **/
@RestController
public class ContentController {
    @Autowired
    private ContentService contentService;


    @GetMapping("/parse/{keyword}")
    public  Boolean parse(@PathVariable("keyword") String keyword) throws Exception {
        return  contentService.pareseContent(keyword);
    }


    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    public List<Map<String,Object>> search(@PathVariable("keyword") String keyword,
                                           @PathVariable("pageNo") int pageNo,
                                             @PathVariable("pageSize") int pageSize  ) throws IOException {

        return  contentService.serchPage(keyword, pageNo, pageSize);
    }

    static List<Map<String, Object>> list = new ArrayList<>();
    static {
        for(int i=0;i<300;i++) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("id", i+1);
            map1.put("value1", "data-"+i);
            map1.put("value2", "other-"+i);
            list.add(map1);
        }
    }

    @RequestMapping("/listData")
    public Map<String,Object>findPage(int pageNum, int pageSize,String data) throws IOException {
        Map<String, Object> map = new HashMap<>();
        System.out.println(data);
        map.put("total", contentService.serchPage(data, pageNum, pageSize).size());
        if(contentService.serchPage(data, pageNum, pageSize).size()<pageSize){
            int tmpPageSize = contentService.serchPage(data, pageNum, pageSize).size();
            map.put("list", contentService.serchPage(data, pageNum, tmpPageSize).subList((pageNum - 1) * tmpPageSize, pageNum * tmpPageSize ));
        }else{
            map.put("list", contentService.serchPage(data, pageNum, pageSize).subList((pageNum - 1) * pageSize, pageNum * pageSize ));
        }
        //modify by grj
        //map.put("total", list.size());
        //map.put("list", list.subList((pageNum - 1) * pageSize, pageNum * pageSize ));
        return map;
    }
    @GetMapping("/startPage")
    public String startPage(){

        return "ok";

    }
}




























